package org.example.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.system.common.result.Result;
import org.example.system.model.dto.FileInfo;
import org.example.system.model.entity.BizCarousel;
import org.example.system.model.form.CarouselForm;
import org.example.system.service.BizCarouselService;
import org.example.system.service.OssService;
import org.example.system.validator.Group;
import org.modelmapper.ModelMapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 轮播图控制器
 *
 * @author khb78
 */
@Tag(name = "08.轮播图接口")
@RestController
@RequestMapping("/api/v1/slides")
@RequiredArgsConstructor
public class BizCarouselController {

    private final BizCarouselService bizCarouselService;

    private final ModelMapper modelMapper;

    private final OssService ossService;

    @Operation(summary = "获取轮播图列表")
    @GetMapping("/list")
    public Result<Object> list(
            @RequestParam(name = "num",required = false,defaultValue = "4") Integer num){
        if(num > 4) {
            num = 4;
        }
        LambdaQueryWrapper<BizCarousel> qw = new LambdaQueryWrapper<>();
        qw.last(String.format("limit %d", num));
        qw.orderByDesc(org.example.system.model.entity.BizCarousel::getCreateTime);
        List<BizCarousel> list = bizCarouselService.list(qw);
        return Result.success(list);
    }

    @Operation(summary = "新增轮播图")
    @PostMapping("/add")
    public Result<Object> add(CarouselForm carouselForm){
        BizCarousel bizCarousel = modelMapper.map(carouselForm, BizCarousel.class);
        FileInfo fileInfo = ossService.uploadFile(carouselForm.getFile());
        bizCarousel.setImageUrl(fileInfo.getUrl());
        bizCarouselService.save(bizCarousel);
        return Result.success("新增成功");
    }

    @Operation(summary = "修改轮播图")
    @PostMapping("/update")
    public Result<Object> update(@Validated(value = {Group.Update.class}) CarouselForm carouselForm){
        BizCarousel bizCarousel = modelMapper.map(carouselForm, BizCarousel.class);
        FileInfo fileInfo = ossService.uploadFile(carouselForm.getFile());
        bizCarousel.setImageUrl(fileInfo.getUrl());
        bizCarouselService.updateById(bizCarousel);
        return Result.success("修改成功");
    }

    @Operation(summary = "删除轮播图")
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable("id") Integer id){
        bizCarouselService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "删除轮播图文件")
    @GetMapping("/deleteFile")
    public Result<Object> deleteFile(String objectName) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        ossService.deleteFile(objectName);
        return Result.success();
    }

    @Operation(summary = "获取轮播图")
    @GetMapping("/get")
    public Result<Object> get(@RequestParam(name = "id") Integer id){
        BizCarousel bizCarousel = bizCarouselService.getById(id);
        return Result.success(bizCarousel);
    }
}
